<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增供应商 </a-button>
        <!--        <a-button type="primary" @click="getUserList"> 测试通讯录 </a-button>-->
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { supplierListApi,supplierDeleteApi } from '@/api/sys/wmProduct';
  import { onMounted } from 'vue';

  import FormModal from './FormModal.vue';

  import { columns, searchFormSchema } from './role.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import { useGo } from '@/hooks/web/usePage';

  const go = useGo();

  defineOptions({ name: 'clazzList' });
  const { createMessage } = useMessage();

  const [register, { openModal, closeModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '供应商列表',
    api: supplierListApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  onMounted(() => {
    // dd.ready(function() {
    //
    // }
  });

  function getUserList() {}

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    supplierDeleteApi(record)
      .then((res) => {
        createMessage.success(res.message);
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        reload();
      });
  }

  function handleSuccess() {
    reload();
  }
</script>
